//
//  PopupTemplate.m
//  TowerDefence
//
//  Created by User on 6/26/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "PopupTemplate.h"

@implementation PopupTemplate

@synthesize delegate, mainSprite;

-(id)initPopupWith:(GameLayer *)_gameLayer {
    self = [super init];
    gameLayer = _gameLayer;
    
    return self;
}

-(void)showPopup {    
    [gameLayer isTouchEnableForAllChild:NO];
    
    CGSize size = [[CCDirector sharedDirector] winSize];
    mainSprite.position = ccp(size.width/2, size.height/2);
    
    backgroundCover = [CCSprite spriteWithFile:BLANK_IMAGE];
    backgroundCover.opacity = 0;
    backgroundCover.color = ccc3(0, 0, 0);
    backgroundCover.position = ccp(240, 160);
    backgroundCover.textureRect = CGRectMake(0, 0, size.width, size.height);
    [gameLayer addChild:backgroundCover z:1000];
    
    [backgroundCover runAction:[CCFadeTo actionWithDuration:0.15 opacity:120]];
    
    mainSprite.scale = 0;
    [gameLayer addChild:mainSprite z:1001];
    [mainSprite runAction:[CCSequence actions:[CCDelayTime actionWithDuration:0.15],[CCScaleTo actionWithDuration:0.25 scale:1.2f],[CCScaleTo actionWithDuration:0.05 scale:1.0f], nil]];
}

-(void)closePopup {
    [mainSprite stopAllActions];
    [mainSprite removeAllChildrenWithCleanup:YES];
    [mainSprite removeFromParentAndCleanup:YES];
    
    [backgroundCover stopAllActions];
    [backgroundCover removeFromParentAndCleanup:YES];
    
    [gameLayer isTouchEnableForAllChild:YES];
}

-(void)cancelButtonClick {
    [self closePopup];
    [delegate popupCancelButtonClick];
}

-(void)okButtonClick {
    [self closePopup];
    [delegate popupOKButtonClick];
}

@end
